var map2 = new AMap.Map('container2', {
    mapStyle: 'amap://styles/blue',
    zoom: 6.33,
    viewMode:"3D",
    center:new AMap.LngLat(107.624225,30.211628)
});

var layer = new Loca.HeatmapLayer({
    map: map2,
});
layer.setData(heatdata, {
    lnglat: 'lnglat',
});

layer.setOptions({
    style: {
        radius: 20,
        color: {
            0.5: '#2c7bb6',
            0.65: '#abd9e9',
            0.7: '#ffffbf',
            0.9: '#fde468',
            1.0: '#d7191c'
        }
    }
});

layer.render();

var colors = [
    // "#05607D",
    "#00FFF0",
    // "#4D4D4D"
    // "#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00",
    // "#b82e2e", "#316395", "#994499", "#22aa99", "#aaaa11", "#6633cc", "#e67300", "#8b0707",
    // "#651067", "#329262", "#5574a6", "#3b3eac"
];

AMapUI.load(['ui/geo/DistrictExplorer', 'lib/$'], function(DistrictExplorer, $) {
    //创建一个实例
    var districtExplorer = window.districtExplorer = new DistrictExplorer({
        eventSupport: true, //打开事件支持
        map: map2
    });
    var node_all;
    //当前聚焦的区域
    var currentAreaNode = null;

    //鼠标hover提示内容
    // var $tipMarkerContent = $("<div class='tipMarker top'></div>");

    var $tipMarkerContent = $('<font face="微软雅黑" size="3" color="#FFFFFF"></font>');

    var tipMarker = new AMap.Marker({
        content: $tipMarkerContent.get(0),
        offset: new AMap.Pixel(0, 0),
        bubble: true,
        zIndex:99
    });

    //根据Hover状态设置相关样式
    function toggleHoverFeature(feature, isHover, position) {

        tipMarker.setMap(isHover ? map : null);

        if (!feature) {
            return;
        }

        var props = feature.properties;

        if (isHover) {

            //更新提示内容
            $tipMarkerContent.html(props.name);
            //更新位置
            tipMarker.setPosition(position || props.center);
        }

        $('#area-tree').find('h2[data-adcode="' + props.adcode + '"]').toggleClass('hover', isHover);

        //更新相关多边形的样式
        var polys = districtExplorer.findFeaturePolygonsByAdcode(props.adcode);
        for (var i = 0, len = polys.length; i < len; i++) {

            polys[i].setOptions({
                fillOpacity: isHover ? 0.5 : 0.2
            });
        }
    }

    //监听feature的hover事件
    districtExplorer.on('featureMouseout featureMouseover', function(e, feature) {
        toggleHoverFeature(feature, e.type === 'featureMouseover',
            e.originalEvent ? e.originalEvent.lnglat : null);
    });

    //监听鼠标在feature上滑动
    districtExplorer.on('featureMousemove', function(e, feature) {
        //更新提示位置
        tipMarker.setPosition(e.originalEvent.lnglat);
    });

    //feature被点击
    districtExplorer.on('featureClick', function(e, feature) {

        var props = feature.properties;

        //如果存在子节点
        if (props.childrenNum > 0) {
            //切换聚焦区域
            switch2AreaNode(props.adcode);
        }
        else{
            // var node =  switch1AreaNode(props.adcode);
            render(node_all);
            map.setZoom(11);
            // var ln = new AMap.LngLat(116.418757,39.917544);
            map.setCenter(props.center);
            tipMarker.setMap(null);
        }
    });

    // //外部区域被点击
    // districtExplorer.on('outsideClick', function(e) {

    //     districtExplorer.locatePosition(e.originalEvent.lnglat, function(error, routeFeatures) {

    //         if (routeFeatures && routeFeatures.length > 1) {
    //             //切换到省级区域
    //             switch2AreaNode(routeFeatures[1].properties.adcode);
    //         } else {
    //             //切换到全国
    //             switch2AreaNode(100000);
    //         }

    //     }, {
    //         levelLimit: 2
    //     });
    // });

    function render(areaNode){
        //更新地图视野
        map.setBounds(areaNode.getBounds(), null, null, true);

        //清除已有的绘制内容
        districtExplorer.clearFeaturePolygons();

        //绘制子区域
        districtExplorer.renderSubFeatures(areaNode, function(feature, i) {

            var fillColor = colors[i % colors.length];
            // var strokeColor = colors[colors.length - 1 - i % colors.length];
            var strokeColor = "#FFFFFF";

            return {
                cursor: 'default',
                bubble: true,
                strokeColor: strokeColor, //线颜色
                strokeOpacity: 1, //线透明度
                strokeWeight: 1, //线宽
                fillColor: null, //填充色
                fillOpacity: 0, //填充透明度
            };
        });

        //绘制父区域
        districtExplorer.renderParentFeature(areaNode, {
            cursor: 'default',
            bubble: true,
            strokeColor: '#FFFFFF', //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 1, //线宽
            fillColor: null, //填充色
            fillOpacity: 0.35, //填充透明度
        });
    }
    //绘制某个区域的边界
    function renderAreaPolygons(areaNode) {
        node_all = areaNode;
        //更新地图视野
        // map.setBounds(areaNode.getBounds(), null, null, true);
        map.setZoom(8.33)
        //清除已有的绘制内容
        districtExplorer.clearFeaturePolygons();

        //绘制子区域
        districtExplorer.renderSubFeatures(areaNode, function(feature, i) {

            // var fillColor = colors[i % colors.length];
            // var strokeColor = colors[colors.length - 1 - i % colors.length];
            var strokeColor = "#FFFFFF";

            return {
                cursor: 'default',
                bubble: true,
                strokeColor: strokeColor, //线颜色
                strokeOpacity: 1, //线透明度
                strokeWeight: 1, //线宽
                fillColor: null, //填充色
                fillOpacity: 0.35, //填充透明度
            };
        });

        //绘制父区域
        districtExplorer.renderParentFeature(areaNode, {
            cursor: 'default',
            bubble: true,
            strokeColor: '#FFFFFF', //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 1, //线宽
            fillColor: null, //填充色
            fillOpacity: 0.35, //填充透明度
        });
    }

    //切换区域后刷新显示内容
    function refreshAreaNode(areaNode) {

        districtExplorer.setHoverFeature(null);

        renderAreaPolygons(areaNode);

        //更新选中节点的class
        var $nodeEles = $('#area-tree').find('h2');

        $nodeEles.removeClass('selected');

        var $selectedNode = $nodeEles.filter('h2[data-adcode=' + areaNode.getAdcode() + ']').addClass('selected');

        //展开下层节点
        $selectedNode.closest('li').removeClass('hide-sub');

        //折叠下层的子节点
        $selectedNode.siblings('ul.sublist').children().addClass('hide-sub');
    }

    //切换区域
    function switch2AreaNode(adcode, callback) {

        if (currentAreaNode && ('' + currentAreaNode.getAdcode() === '' + adcode)) {
            return;
        }

        loadAreaNode(adcode, function(error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                return;
            }

            currentAreaNode = window.currentAreaNode = areaNode;

            //设置当前使用的定位用节点
            districtExplorer.setAreaNodesForLocating([currentAreaNode]);

            refreshAreaNode(areaNode);

            if (callback) {
                callback(null, areaNode);
            }
        });
    }
    //加载区域
    function loadAreaNode(adcode, callback) {

        districtExplorer.loadAreaNode(adcode, function(error, areaNode) {

            if (error) {

                if (callback) {
                    callback(error);
                }

                console.error(error);

                return;
            }

            if (callback) {
                callback(null, areaNode);
            }
        });
    }

    //重庆
    switch2AreaNode(500000);
});